<?php

/**
 * zTinyMce actions.
 *
 * @package    sf_sandbox
 * @subpackage zTinyMce
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class zTinyMceActions extends sfActions
{
    /**
     * Executes index action
     *
     */
    public function executeIndex()
    {
    }

    public function executeDynamicGenerateImageList()
    {
        $output = ''; // Here we buffer the JavaScript code we want to send to the browser.
        $delimiter = ""; // for eye candy... code gets new lines

        $output .= 'var tinyMCEImageList = new Array(';
        //$directory = "/uploads/product/"; // Use your correct (relative!) path here
        $directory = sfConfig::get('sf_upload_dir') . "/products/"; // Use your correct (relative!) path here

        // Since TinyMCE3.x you need absolute image paths in the list...
        $abspath = preg_replace('~^/?(.*)/[^/]+$~', '/$1', $_SERVER['SCRIPT_NAME']);
        //var_dump($directory);    //E:\xampplite\htdocs\walao\web\uploads/products/
        if (is_dir($directory)) {
            $direc = opendir($directory);
            while ($file = readdir($direc)) {
                if ($this->getFileExtension($file) && (strtoupper($this->getFileExtension($file)) == "JPG" || strtoupper($this->getFileExtension($file)) == "JEPG") || strtoupper($this->getFileExtension($file)) == "PNG" || strtoupper($this->getFileExtension($file)) == "GIF") {
                    if (is_file("$directory/$file")) {
                        // We got ourselves a file! Make an array entry:
                        $output .= $delimiter
                                   . '["'
                                   . utf8_encode($file)
                                   . '", "'
                                   . utf8_encode("/uploads/products/$file")
                                   //. utf8_encode("$abspath/$directory/$file")
                                   . '"],';
                    }
                }
            }

            $output = substr($output, 0, -1); // remove last comma from array item list (breaks some browsers)
            $output .= $delimiter;
            closedir($direc);
        }
        // Finish code: end of array definition. Now we have the JavaScript code ready!
        $output .= ');';
        echo $output;

        return sfView::HEADER_ONLY;
    }

    function getFileExtension($file_name)
    {
        return substr(strrchr($file_name, '.'), 1);
    }
}
